<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use App\Models\DateCollection;
use App\Models\FlowCollection;

class DataAnalysisAPIController extends AppBaseController
{
    /**
     * 收入金额分析
     *
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $input = $request->all();

        $park_arr = $this->getUserParks();
        $park_arr = $this->getParkCodes($park_arr);

        // 获取车场记录
        $d = new DateCollection();

        $common_model = $d;  // 本期

        // 车场名称
        if(isset($input['park_code']) && !empty($input['park_code'])){
            $common_model =  $common_model->whereIn("park_code", $input['park_code']);
        } else {
            $common_model =  $common_model->whereIn("park_code", $park_arr);
        }
        $all_model = $common_model;
        // 往期时间
        if(isset($input['last_time']) && !empty($input['last_time'])) {
            $last_time = $input['last_time'];
            if (isset($last_time[0]) && !empty($last_time[0])) {
                $start_time = date("Y-m-d H:i:s",$last_time[0]);
                $all_model = $all_model->where("hour", '>=', $start_time);
            }

            if (isset($last_time[1]) && !empty($last_time[1])) {
                $start_time = date("Y-m-d H:i:s", $last_time[1]);
                $all_model = $all_model->where("hour", '<', $start_time);
            }
        }

        $car_type = config('custom.car_type');
        $pay_type = config('custom.pay_type');
        $all_feild  = '';
        $all_mfeild  = '';
        $cash_feild_count = '';
        $cash_feild_amount = '';
        $wechat_feild_count = '';
        $wechat_feild_amount = '';
        $alipay_feild_count = '';
        $alipay_feild_amount = '';
        $union_feild_count = '';
        $union_feild_amount = '';
        $etc_feild_count = '';
        $etc_feild_amount = '';
        $other_feild_count = '';
        $other_feild_amount = '';
        foreach ($car_type as $key=>$val) {
            foreach ($pay_type as $k=>$v) {
                $all_feild .= $key."_".$k."_count+";
                $all_mfeild .= $key."_".$k."_amount+";
                if($k == 'cash'){
                    $cash_feild_count .= $key."_".$k."_count+";
                    $cash_feild_amount .= $key."_".$k."_amount+";
                }
                if($k == 'wechat'){
                    $wechat_feild_count .= $key."_".$k."_count+";
                    $wechat_feild_amount .= $key."_".$k."_amount+";
                }
                if($k == 'alipay'){
                    $alipay_feild_count .= $key."_".$k."_count+";
                    $alipay_feild_amount .= $key."_".$k."_amount+";
                }
                if($k == 'union'){
                    $union_feild_count .= $key."_".$k."_count+";
                    $union_feild_amount .= $key."_".$k."_amount+";
                }
                if($k == 'etc'){
                    $etc_feild_count .= $key."_".$k."_count+";
                    $etc_feild_amount .= $key."_".$k."_amount+";
                }
                if($k == 'other'){
                    $other_feild_count .= $key."_".$k."_count+";
                    $other_feild_amount .= $key."_".$k."_amount+";
                }
            }
        }

        $all_feild = trim($all_feild,"+");
        $all_mfeild = trim($all_mfeild,"+");
        $cash_feild_count = trim($cash_feild_count,"+");
        $cash_feild_amount = trim($cash_feild_amount,"+");
        $wechat_feild_count = trim($wechat_feild_count,"+");
        $wechat_feild_amount = trim($wechat_feild_amount,"+");
        $alipay_feild_count = trim($alipay_feild_count,"+");
        $alipay_feild_amount = trim($alipay_feild_amount,"+");
        $union_feild_count = trim($union_feild_count,"+");
        $union_feild_amount = trim($union_feild_amount,"+");
        $etc_feild_count = trim($etc_feild_count,"+");
        $etc_feild_amount = trim($etc_feild_amount,"+");
        $other_feild_count = trim($other_feild_count,"+");
        $other_feild_amount = trim($other_feild_amount,"+");
        $all_res = $all_model->first(
            [
            \DB::raw("SUM($all_mfeild) as all_amount"),
            \DB::raw("SUM($all_feild) as all_count"),
            \DB::raw("SUM(cash_count) as cash_count"),
            \DB::raw("SUM(cash_amount) as cash_amount"),
            \DB::raw("SUM(wechat_count) as wechat_count"),
            \DB::raw("SUM(wechat_amount) as wechat_amount"),
            \DB::raw("SUM(alipay_count) as alipay_count"),
            \DB::raw("SUM(alipay_amount) as alipay_amount"),
            \DB::raw("SUM(union_count) as union_count"),
            \DB::raw("SUM(union_amount) as union_amount"),
            \DB::raw("SUM(etc_count) as etc_count"),
            \DB::raw("SUM(etc_amount) as etc_amount"),
            \DB::raw("SUM(other_count) as other_count"),
            \DB::raw("SUM(other_amount) as other_amount"),
            ]
        );

        $all_res = obj2arr($all_res);
        // 本期
        // 获取车场记录
        $d = new DateCollection();

        $common_model = $d;  // 本期

        // 车场名称
        if(isset($input['park_code']) && !empty($input['park_code'])){
            $common_model =  $common_model->whereIn("park_code", $input['park_code']);
        } else {
            $common_model =  $common_model->whereIn("park_code", $park_arr);
        }
        $nall_model = $common_model;
        // 本期时间
        if(isset($input['now_time']) && !empty($input['now_time'])) {
            $now_time = $input['now_time'];
            if (isset($now_time[0]) && !empty($now_time[0])) {
                $start_time = date("Y-m-d H;i:s",$now_time[0]);
                $nall_model = $nall_model->where("hour", '>=', $start_time);
            }
            if (isset($now_time[1]) && !empty($now_time[1])) {
                $start_time = date("Y-m-d H;i:s",$now_time[1]);
                $nall_model = $nall_model->where("hour", '<', $start_time);
            }
        }

        $nall_res = $nall_model->first(
            [
                \DB::raw("SUM($all_mfeild) as all_amount"),
                \DB::raw("SUM($all_feild) as all_count"),
                \DB::raw("SUM(cash_count) as cash_count"),
                \DB::raw("SUM(cash_amount) as cash_amount"),
                \DB::raw("SUM(wechat_count) as wechat_count"),
                \DB::raw("SUM(wechat_amount) as wechat_amount"),
                \DB::raw("SUM(alipay_count) as alipay_count"),
                \DB::raw("SUM(alipay_amount) as alipay_amount"),
                \DB::raw("SUM(union_count) as union_count"),
                \DB::raw("SUM(union_amount) as union_amount"),
                \DB::raw("SUM(etc_count) as etc_count"),
                \DB::raw("SUM(etc_amount) as etc_amount"),
                \DB::raw("SUM(other_count) as other_count"),
                \DB::raw("SUM(other_amount) as other_amount"),
            ]
        );
        $nall_res = obj2arr($nall_res);
        $res = [];
        $title = ['all'=>'合计','cash'=>'现金','wechat'=>'微信','alipay'=>'支付宝','union'=>'银联','etc'=>'ETC','other'=>'其他',];
        foreach ($title as $k=>$v) {
            $ckey = $k."_count";
            $akey = $k."_amount";
            $res[$k]['title'] = $title[$k];
            $res[$k]['count'] = $all_res[$ckey]??0;
            $res[$k]['amount'] = re2float($all_res[$akey]/100);
            $res[$k]['ncount'] = $nall_res[$ckey]??0;
            $res[$k]['namount'] = re2float($nall_res[$akey]/100);
            $res[$k]["cper"] = $all_res[$ckey]>0?re2float(round(($nall_res[$ckey]-$all_res[$ckey])/$all_res[$ckey]*100,2))."%":'0.00%';
            $res[$k]["per"] = $all_res[$akey]>0?re2float(round(($nall_res[$akey]-$all_res[$akey])/$all_res[$akey]*100,2))."%":'0.00%';
        }
        return $this->sendResponse($res, '查询成功');
    }


    /**
     * 停车流量分析
     *
     * @param Request $request
     * @return mixed
     */
    public function flowTotal(Request $request)
    {
        $input = $request->all();

        // 获取车场记录
        $d = new FlowCollection();

        $common_model = $d;  // 本期

        // 车场名称
        if(isset($input['park_code']) && !empty($input['park_code'])){
            $common_model =  $common_model->whereIn("park_code", $input['park_code']);
        }
        $all_model = $common_model;
        // 往期时间
        if(isset($input['last_time']) && !empty($input['last_time'])) {
            $last_time = $input['last_time'];
            if (isset($last_time[0]) && !empty($last_time[0])) {
                $start_time = date("Y-m-d H;i:s",$last_time[0]);
                $all_model = $all_model->where("hour", '>=', $start_time);
            }
            if (isset($last_time[1]) && !empty($last_time[1])) {
                $start_time = date("Y-m-d H;i:s",$last_time[1]);
                $all_model = $all_model->where("hour", '<', $start_time);
            }
        }

        $all_res = $all_model->first(
            [
                \DB::raw("SUM(free_car_total) as free_car"),
                \DB::raw("SUM(total_charge_car) as charge_car"),
            ]
        );
        $all_res = obj2arr($all_res);

        // 本期
        $d = new FlowCollection();

        $common_model = $d;  // 本期

        // 车场名称
        if(isset($input['park_code']) && !empty($input['park_code'])){
            $common_model =  $common_model->whereIn("park_code", $input['park_code']);
        }
        $nall_model = $common_model;
        // 本期时间
        if(isset($input['now_time']) && !empty($input['now_time'])) {
            $now_time = $input['now_time'];
            if (isset($now_time[0]) && !empty($now_time[0])) {
                $start_time = date("Y-m-d H;i:s",$now_time[0]);
                $nall_model = $nall_model->where("hour", '>=', $start_time);
            }
            if (isset($now_time[1]) && !empty($now_time[1])) {
                $start_time = date("Y-m-d H;i:s",$now_time[1]);
                $nall_model = $nall_model->where("hour", '<=', $start_time);
            }
        }

        $nall_res = $nall_model->first(
            [
                \DB::raw("SUM(free_car_total) as nfree_car"),
                \DB::raw("SUM(total_charge_car) as ncharge_car"),
            ]
        );
        $nall_res = obj2arr($nall_res);
        $res = [];
        $title = ['all'=>'合计','free'=>'收费车','charge'=>'免费车'];
        $ntotal = $nall_res['nfree_car']+$nall_res['ncharge_car'];
        $total = $all_res['free_car']+$all_res['charge_car'];
        $res = [
            ['合计',$total, $ntotal, $total>0?re2float(($ntotal-$total)/$total*100)."%":'0.00%'],
            ['收费车',$all_res['charge_car'], $nall_res['ncharge_car'],$all_res['charge_car']>0?re2float(($nall_res['ncharge_car']-$all_res['charge_car'])/$all_res['charge_car']*100)."%":'0.00%'],
            ['免费车',$all_res['free_car'], $nall_res['nfree_car'],$all_res['free_car']?re2float(($nall_res['nfree_car']-$all_res['free_car'])/$all_res['free_car']*100)."%":'0.00%']
        ];
        return $this->sendResponse($res, '查询成功');
    }

    /**
     * 收费车流量汇总
     *
     * @param Request $request
     * @return mixed
     */
    public function ChargeTotal(Request $request)
    {
        $input = $request->all();

        // 获取车场记录
        $d = new FlowCollection();

        $common_model = $d;  // 本期

        // 车场名称
        if(isset($input['park_code']) && !empty($input['park_code'])){
            $common_model =  $common_model->whereIn("park_code", $input['park_code']);
        }
        $all_model = $common_model;
        // 往期时间
        if(isset($input['last_time']) && !empty($input['last_time'])) {
            $last_time = $input['last_time'];
            if (isset($last_time[0]) && !empty($last_time[0])) {
                $start_time = date("Y-m-d H;i:s",$last_time[0]);
                $all_model = $all_model->where("hour", '>=', $start_time);
            }
            if (isset($last_time[1]) && !empty($last_time[1])) {
                $start_time = date("Y-m-d 00:00:00",$last_time[1]);
                $all_model = $all_model->where("hour", '<=', $start_time);
            }
        }

        $all_res = $all_model->first(
            [
                \DB::raw("SUM(small_car) as small_car"),
                \DB::raw("SUM(taxi) as taxi"),
                \DB::raw("SUM(big_car) as big_car"),
                \DB::raw("SUM(other_charge_car) as other_charge_car"),
            ]
        );
        $all_res = obj2arr($all_res);

        // 本期
        $d = new FlowCollection();

        $common_model = $d;  // 本期

        // 车场名称
        if(isset($input['park_code']) && !empty($input['park_code'])){
            $common_model =  $common_model->whereIn("park_code", $input['park_code']);
        }
        $nall_model = $common_model;
        // 本期时间
        if(isset($input['now_time']) && !empty($input['now_time'])) {
            $now_time = $input['now_time'];
            if (isset($now_time[0]) && !empty($now_time[0])) {
                $start_time = date("Y-m-d H;i:s",$now_time[0]);
                $nall_model = $nall_model->where("hour", '>=', $start_time);
            }
            if (isset($now_time[1]) && !empty($now_time[1])) {
                $start_time = date("Y-m-d H;i:s",$now_time[1]);
                $nall_model = $nall_model->where("hour", '<=', $start_time);
            }
        }

        $nall_res = $nall_model->first(
            [
                \DB::raw("SUM(small_car) as small_car"),
                \DB::raw("SUM(taxi) as taxi"),
                \DB::raw("SUM(big_car) as big_car"),
                \DB::raw("SUM(other_charge_car) as other_charge_car"),
            ]
        );
        $nall_res = obj2arr($nall_res);

        $res = [];
        $title = ['all'=>'合计','free'=>'小车','charge'=>'大车','charge'=>'出粗车','charge'=>'其他'];
        $ntotal = $nall_res['small_car']+$nall_res['taxi']+$nall_res['big_car']+$nall_res['other_charge_car'];
        $total = $all_res['small_car']+$all_res['taxi']+$all_res['big_car']+$all_res['other_charge_car'];
        $res = [
            ['合计',$total, $ntotal, $total>0?re2float(($ntotal-$total)/$total*100)."%":'0.00%'],
            ['小车',$all_res['taxi'], $nall_res['taxi'],$all_res['taxi']>0?re2float(($nall_res['taxi']-$all_res['taxi'])/$all_res['taxi']*100)."%":'0.00%'],
            ['大车',$all_res['big_car'], $nall_res['big_car'],$all_res['big_car']>0?re2float(($nall_res['big_car']-$all_res['big_car'])/$all_res['big_car']*100)."%":'0.00%'],
            ['出租',$all_res['taxi'], $nall_res['taxi'],$all_res['taxi']>0?re2float(($nall_res['taxi']-$all_res['taxi'])/$all_res['taxi']*100)."%":'0.00%'],
            ['其他',$all_res['other_charge_car'], $nall_res['other_charge_car'],$all_res['other_charge_car']>0?re2float(($nall_res['other_charge_car']-$all_res['other_charge_car'])/$all_res['other_charge_car']*100)."%":'0.00%'],
        ];
        return $this->sendResponse($res, '查询成功');
    }

    /**
     * 免费车流量汇总
     *
     * @param Request $request
     * @return mixed
     */
    public function FreeTotal(Request $request)
    {
        $input = $request->all();

        // 获取车场记录
        $d = new FlowCollection();

        $common_model = $d;  // 本期

        // 车场名称
        if(isset($input['park_code']) && !empty($input['park_code'])){
            $common_model =  $common_model->whereIn("park_code", $input['park_code']);
        }
        $all_model = $common_model;
        // 往期时间
        if(isset($input['last_time']) && !empty($input['last_time'])) {
            $last_time = $input['last_time'];
            if (isset($last_time[0]) && !empty($last_time[0])) {
                $start_time = date("Y-m-d H;i:s",$last_time[0]);
                $all_model = $all_model->where("hour", '>=', $start_time);
            }
            if (isset($last_time[1]) && !empty($last_time[1])) {
                $start_time = date("Y-m-d H;i:s",$last_time[1]);
                $all_model = $all_model->where("hour", '<=', $start_time);
            }
        }

        $all_res = $all_model->first(
            [
                \DB::raw("SUM(short_time_free) as short_time_free"),
                \DB::raw("SUM(self_free) as self_free"),
                \DB::raw("SUM(worker_car) as worker_car"),
                \DB::raw("SUM(inner_car) as inner_car"),
                \DB::raw("SUM(free_car) as free_car"),
                \DB::raw("SUM(blank_car) as blank_car"),
                \DB::raw("SUM(other_free) as other_free"),
            ]
        );
        $all_res = obj2arr($all_res);

        // 本期
        $d = new FlowCollection();

        $common_model = $d;  // 本期

        // 车场名称
        if(isset($input['park_code']) && !empty($input['park_code'])){
            $common_model =  $common_model->whereIn("park_code", $input['park_code']);
        }
        $nall_model = $common_model;
        // 本期时间
        if(isset($input['now_time']) && !empty($input['now_time'])) {
            $now_time = $input['now_time'];
            if (isset($now_time[0]) && !empty($now_time[0])) {
                $start_time = date("Y-m-d H;i:s",$now_time[0]);
                $nall_model = $nall_model->where("hour", '>=', $start_time);
            }
            if (isset($now_time[1]) && !empty($now_time[1])) {
                $start_time = date("Y-m-d H;i:s",$now_time[1]);
                $nall_model = $nall_model->where("hour", '<=', $start_time);
            }
        }

        $nall_res = $nall_model->first(
            [
                \DB::raw("SUM(short_time_free) as short_time_free"),
                \DB::raw("SUM(self_free) as self_free"),
                \DB::raw("SUM(worker_car) as worker_car"),
                \DB::raw("SUM(inner_car) as inner_car"),
                \DB::raw("SUM(free_car) as free_car"),
                \DB::raw("SUM(blank_car) as blank_car"),
                \DB::raw("SUM(other_free) as other_free"),
            ]
        );
        $nall_res = obj2arr($nall_res);

        $res = [];
        $title = ['all'=>'合计','short_time_free'=>'短时免费','self_free'=>'手动抬杆','worker_car'=>'员工车','inner_car'=>'内部车','free_car'=>'免费车','blank_car'=>'白名单','other_free'=>'其他'];
        $ntotal = $nall_res['short_time_free']+$nall_res['self_free']+$nall_res['worker_car']+$nall_res['inner_car']+$nall_res['free_car']+$nall_res['blank_car']+$nall_res['other_free'];
        $total = $all_res['short_time_free']+$all_res['self_free']+$all_res['worker_car']+$all_res['inner_car']+$all_res['free_car']+$all_res['blank_car']+$all_res['other_free'];
        $res = [
            ['合计',$total, $ntotal, $total>0?re2float(($ntotal-$total)/$total*100,2)."%":'0.00%'],
            ['短时免费',$all_res['short_time_free'], $nall_res['short_time_free'],$all_res['short_time_free']>0?re2float(($nall_res['short_time_free']-$all_res['short_time_free'])/$all_res['short_time_free']*100)."%":'0.00%'],
            ['手动抬杆',$all_res['self_free'], $nall_res['self_free'],$all_res['self_free']>0?re2float(($nall_res['self_free']-$all_res['self_free'])/$all_res['self_free']*100)."%":'0.00%'],
            ['员工车',$all_res['worker_car'], $nall_res['worker_car'],$all_res['worker_car']>0?re2float(($nall_res['worker_car']-$all_res['worker_car'])/$all_res['worker_car']*100)."%":'0.00%'],
            ['内部车',$all_res['inner_car'], $nall_res['inner_car'],$all_res['inner_car']>0?re2float(($nall_res['inner_car']-$all_res['inner_car'])/$all_res['inner_car']*100)."%":'0.00%'],
            ['免费车',$all_res['free_car'], $nall_res['free_car'],$all_res['free_car']>0?re2float(($nall_res['free_car']-$all_res['free_car'])/$all_res['free_car']*100)."%":'0.00%'],
            ['白名单',$all_res['blank_car'], $nall_res['blank_car'],$all_res['blank_car']>0?re2float(($nall_res['blank_car']-$all_res['blank_car'])/$all_res['blank_car']*100)."%":'0.00%'],
            ['其他',$all_res['other_free'], $nall_res['other_free'],$all_res['other_free']>0?re2float(($nall_res['other_free']-$all_res['other_free'])/$all_res['other_free']*100)."%":'0.00%'],
        ];
        return $this->sendResponse($res, '查询成功');
    }
}
